package com.fixme4trade.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import com.fixme4trade.constant.Role;

@Entity
public class User implements br.gov.frameworkdemoiselle.security.User, Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	//@GeneratedValue
	@SequenceGenerator( name = "USER_ID", sequenceName = "USER_SEQ", allocationSize = 1 )  
	@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "USER_ID" )
	@Column(name="user_id")
	private Long userId;

	//@NotNull
	//@NotEmpty
	//@Size(max = 15)
	//@Column(unique = true)
	private String username;

	//@NotNull
	//@NotEmpty
	//@Size(max = 5)
	private String password;
	
	//@Email
	//@NotNull
	//@NotEmpty
	//@Column(unique = true)
	private String eMail;
	
	//@NotNull
	//@NotEmpty
	//@Size(max = 40)
	private String firstName;

	//@NotNull
	//@NotEmpty
	//@Size(max = 40)		
	private String lastName;

	@Temporal(TemporalType.DATE)
	private Date birthDate;		
	
	@Transient
	private Map<Object, Object> attributes;

	@Embedded
	private Address address = new Address();
		
    @ElementCollection(targetClass=Role.class)
    @CollectionTable(name="user_roles")    
    @Column(name = "role_id")
    @Enumerated(EnumType.STRING)
	private List<Role> roleList = new ArrayList<Role>();

	public User() {

	}

	@Override
	public String getId() {
		return this.userId+"";
	}

	public void setId(String id) {
		this.userId = Long.parseLong(id);
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public Object getAttribute(Object key) {
		return attributes.get(key);		
	}

	@Override
	public void setAttribute(Object key, Object value) {
		this.attributes.put(key, value);		
	}

	public String geteMail() {
		return eMail;
	}

	public void seteMail(String eMail) {
		this.eMail = eMail;
	}	

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(final Long userId) {
		this.userId = userId;
	}

	public void addRole(final Role role) {
		List<Role> roleList = getRoleList();
		if (!roleList.contains(role)) {
			roleList.add(role);
		}
			
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(final String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(final String lastName) {
		this.lastName = lastName;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(final Address address) {
		this.address = address;
	}	
}
